//
//  JSSPictureInfoView.swift
//  WeiBoItem
//
//  Created by jss on 16/4/22.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit
import SnapKit

//每个cell之间的间距
let JSSPictureViewItemMargin:CGFloat = 4

let w = Int(SCREENW - 2 * JSSStatusMargin - 2 * JSSPictureViewItemMargin) / 3
//每一个item的宽高
let JSSPictureInfoViewItemWH = CGFloat(w)

//let JSSPictureInfoViewItemWH:CGFloat = (SCREENW - 2 * JSSStatusMargin - 2 * JSSPictureViewItemMargin) / 3

class JSSPictureInfoView: UICollectionView {
    
    
    //重用标识符
    let indentifierID = "collectionViewCell"
    
    //配图控件显示的配图数据
    var pic_urls:[JSSPictureInfoModel]?{
    
        didSet {
            lable.text = "\(pic_urls?.count ?? 0)"
            
            //
            
            //刷新
            self.reloadData()
            
        }
    
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI () {
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        //注册cell
        registerClass(JSSPictureViewCell.self, forCellWithReuseIdentifier: indentifierID)
        
        //设置当前uicollectionview的数据源代理为当前view(self)
        self.dataSource = self
        
        addSubview(lable)
        
        //设置每一个item大小
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: JSSPictureInfoViewItemWH, height: JSSPictureInfoViewItemWH)
        
        //print("大小\(layout.itemSize)")
        
        
        //设置每个item之间的间距
        layout.minimumInteritemSpacing = JSSPictureViewItemMargin
        layout.minimumLineSpacing = JSSPictureViewItemMargin
        
        //约束
        lable.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
    }
    
    
    //加个lable
    private lazy var lable:UILabel = {
    
    let lab = UILabel(textColor: UIColor.darkGrayColor(), textFont: 25)
        
        lab.font =  UIFont.boldSystemFontOfSize(25)
        
        return lab
        
    }()
    
}

//自定义Collectionviewcell
class JSSPictureViewCell:UICollectionViewCell {
    
    var pictureInfo:(JSSPictureInfoModel)? {
        
        didSet {
            
            //给imgeview赋值
            imageView.sd_setImageWithURL(NSURL(string: (pictureInfo?.thumbnail_pic) ?? ""), placeholderImage: UIImage(named: "timeline_image_gif"))

            
            //判断是否有GIF图片
            if let url = pictureInfo?.thumbnail_pic where url.hasSuffix(".gif") {
                
                gifImageView.hidden = false
            }else {
                
                gifImageView.hidden = true
            }
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        //把imageView添加到cell的contentView上
       self.contentView.addSubview(imageView)
        //添加gif图标
        self.contentView.addSubview(gifImageView)
        
        //约束imageView
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.bottom.equalTo(contentView)
        }
        //约束gifImageView
        gifImageView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.trailing.equalTo(contentView)
        }
        
    }
    
    //懒加载imageView
    private lazy var imageView:UIImageView = {
        
        let imageview = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        //设置图片内容填充的模式
        imageview.contentMode = .ScaleAspectFill
        
        //把超出父容器的多余部分剪裁
        imageview.clipsToBounds = true
        
        return imageview
     
    }()
    
    private lazy var gifImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    
}



//专门用来写collectionViewcell数据源方法的分类
extension JSSPictureInfoView: UICollectionViewDataSource {
    
    //每一组有多少个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.pic_urls?.count ?? 0
    }
    
    
    //每个cell的内容
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(indentifierID, forIndexPath: indexPath) as! JSSPictureViewCell
        
        
        cell.pictureInfo = pic_urls![indexPath.item]
//        cell.backgroundColor = UIColor.redColor()
        
        return cell
        
        
    }
    
}
